/**
 * @author: zhangxiang
 */

var gulp = require('gulp');
var connect = require('gulp-connect');
var less = require('gulp-less');
var del = require('del');
var lessAutoprefix = require('less-plugin-autoprefix');
var autoprefix = new lessAutoprefix({browsers: ['last 5 versions'], cascade: true});

gulp.task('connect', function() {
    connect.server({
        root: './',
        port: 8080,
        livereload: true
    });
});

gulp.task('clean:css', function(){
  del.sync('./src/css/*.css');
});

gulp.task('taskLess', ['clean:css'], function(){
	gulp.src('./less/*.less')
		.pipe(less({
			plugins: [autoprefix]
		}))
		.pipe(gulp.dest('src/css'))
		.pipe(connect.reload());
});

gulp.task('watchHtml',function(){
	gulp.src('./*.html')
		.pipe(connect.reload());
});

gulp.task('watchJS',function(){
	gulp.src('./src/js/*.js')
		.pipe(connect.reload());
});

gulp.task('watch', function(){
	gulp.watch('less/*.less', ['taskLess']);
	gulp.watch('*.html',['watchHtml']);	
	gulp.watch('src/js/*.js',['watchJS']);
});

gulp.task('default', ['taskLess' , 'connect', 'watch']);